<!--
 * @Description: 
 * @Version: 1.0
 * @Autor: younghxp
 * @Date: 2021-11-08 11:01:15
 * @LastEditors: younghxp
 * @LastEditTime: 2022-03-11 16:23:49
-->
<template>
  <div class="mainCon" ref="mainCon">
    <el-row
      class="toolbar searchInputWidth"
      style="padding-bottom: 0; margin-top: 24px !important"
      v-if="userType == 'MONITOR'"
    >
      <el-form
        :inline="true"
        :model="filterField"
        ref="searchConditions"
        label-width="90px"
      >
        <el-col :lg="8" :xl="6">
          <el-form-item label="项目名称" class="row-padding-bottom">
            <el-input
              v-model="filterField.projectName"
              placeholder="请输入项目名称"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :xl="6">
          <el-form-item label="区域" class="row-padding-bottom">
            <el-cascader
              ref="manageArea"
              v-model="areaId"
              :append-to-body="false"
              :options="areasOptions"
              @change="getAreaId"
              clearable
              filterable
              :props="{ checkStrictly: true }"
            ></el-cascader>
          </el-form-item>
        </el-col>
        <template v-if="advanced">
          <el-col :lg="8" :xl="6">
            <el-form-item label="行业" class="row-padding-bottom">
              <el-select
                v-model="filterField.industryId"
                filterable
                placeholder="请选择"
                clearable
                :popper-append-to-body="false"
              >
                <el-option
                  v-for="item in industryOptions"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="投资类型" class="row-padding-bottom">
              <el-select
                v-model="filterField.investment"
                filterable
                placeholder="请选择"
                clearable
                :popper-append-to-body="false"
              >
                <el-option
                  v-for="item in investmentOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="项目状态" class="row-padding-bottom">
              <el-select
                v-model="filterField.projectStatus"
                filterable
                placeholder="请选择"
                clearable
                :popper-append-to-body="false"
              >
                <el-option
                  v-for="item in statusOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="申请日期" class="row-padding-bottom">
              <timeSelection
                :startTime="filterField.applyDateStart"
                :endTime="filterField.applyDateEnd"
                @getStartTimeValue="getStartTimeValue"
                @getEndTimeValue="getEndTimeValue"
              ></timeSelection>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="理赔状态" class="row-padding-bottom">
              <el-select
                v-model="filterField.claimsStatus"
                filterable
                placeholder="请选择"
                clearable
                :popper-append-to-body="false"
              >
                <el-option
                  v-for="item in orderStatusOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="保函类型" class="row-padding-bottom">
              <el-select
                v-model="filterField.insureType"
                filterable
                placeholder="请选择"
                clearable
                :popper-append-to-body="false"
              >
                <el-option
                  v-for="item in typeOptions"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="承保机构" class="row-padding-bottom">
              <el-select
                v-model="filterField.insureOrg"
                filterable
                placeholder="请选择"
                clearable
                :popper-append-to-body="false"
              >
                <el-option
                  v-for="(item, index) in guaranteeInstitutionOptions"
                  :key="index"
                  :label="item.name"
                  :value="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="投保订单号" class="row-padding-bottom">
              <el-input
                v-model="filterField.insureOrder"
                placeholder="请输入投保订单号"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="理赔订单号" class="row-padding-bottom">
              <el-input
                v-model="filterField.claimsOrder"
                placeholder="请输入理赔订单号"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </template>
        <el-form-item label="">
          <a
            @click="toggleAdvanced"
            style="margin-left: 40px; color: #409eff; cursor: pointer"
            class="btnColor"
          >
            {{ advanced ? '收起' : '展开' }}
            <i
              :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
            ></i>
          </a>
          <el-button
            type="primary"
            icon="icon iconfont iconchazhao1"
            @click="searchList"
            class="searchBtn"
          >
            <span>搜索</span>
          </el-button>
        </el-form-item>
        <el-form-item>
          <exportBtn
            :dataLen="tableList.length"
            :filterField="filterField"
            api="api-d/depositInsClaims/expand/export"
            responseType="get"
          ></exportBtn>
        </el-form-item>
      </el-form>
    </el-row>
    <div class="spacing" v-if="userType == 'MONITOR'"></div>
    <div
      ref="tableBox"
      :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
    >
      <el-table
        ref="tableBox1"
        @sort-change="sortChange"
        :max-height="tableHeight"
        :data="tableList"
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
        highlight-current-row
        v-loading="loading"
        stripe
        style="border: 1px solid #dfe6ec"
      >
        <el-table-column
          prop="insureOrder"
          label="订单号"
          align="center"
          :formatter="formatTd"
          min-width="220"
        ></el-table-column>
        <el-table-column
          prop="claimsOrder"
          label="理赔单号"
          align="center"
          :formatter="formatTd"
          min-width="220"
        ></el-table-column>
        <el-table-column label="项目名称" width="230">
          <template slot-scope="scope">
            <copyText
              :tableIndex="scope.$index"
              :copyTextName="scope.row.projectName"
              :columnName="'projectName'"
            ></copyText>
          </template>
        </el-table-column>
        <el-table-column label="施工方" width="230">
          <template slot-scope="scope">
            <copyText
              :tableIndex="scope.$index"
              :copyTextName="scope.row.pbName"
              :columnName="'pbName'"
            ></copyText>
          </template>
        </el-table-column>
        <el-table-column
          prop="insureTypeStr"
          label="保函类型"
          align="center"
          :formatter="formatTd"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="insureOrgStr"
          label="承保机构"
          align="left"
          :formatter="formatTd"
          width="230"
        >
          <template slot-scope="scope">
            <copyText
              :tableIndex="scope.$index"
              :copyTextName="scope.row.insureOrgStr"
              :columnName="'insureOrgStr'"
            ></copyText>
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="申请日期"
          sortable="custom"
          align="center"
          :formatter="formatTd"
          min-width="200"
        ></el-table-column>
        <el-table-column
          prop="linkName"
          label="理赔联系人"
          align="center"
          :formatter="formatTd"
          min-width="120"
        ></el-table-column>
        <el-table-column
          prop="linkTel"
          label="联系电话"
          align="center"
          :formatter="formatTd"
          min-width="120"
        ></el-table-column>
        <el-table-column label="理赔原因" :formatter="formatTd" width="230">
          <template slot-scope="scope">
            <copyText
              :tableIndex="scope.$index"
              :copyTextName="scope.row.claimsReasonStr"
              :columnName="'claimsReasonStr'"
            ></copyText>
          </template>
        </el-table-column>
        <el-table-column
          prop="claimsAmount"
          sortable="custom"
          label="理赔金额（元）"
          align="center"
          :formatter="formatTd"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="statusDesc"
          label="理赔状态"
          align="center"
          :formatter="formatTd"
          min-width="120"
        ></el-table-column>
        <el-table-column label="操作" align="left" fixed="right" width="130px">
          <template slot-scope="scope">
            <el-link type="primary" @click="toDetail(scope.row)">详情</el-link>
            <el-link
              type="primary"
              @click="editClaims(scope.row)"
              v-if="bxUserType == 'JC' && scope.row.statusDesc == '材料复核中'"
              >编辑</el-link
            >
          </template>
        </el-table-column>
        <div slot="empty" v-show="isShow">
          <emptyTable emptyText="暂无相关信息"></emptyTable>
        </div>
      </el-table>
      <Pagination
        ref="pagination"
        @getList="getTableList"
        :filterField="filterField"
        :requestUrl="requestUrl"
        @getpageNo="getpageNo"
        @getpageSize="getpageSize"
      ></Pagination>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  name: 'claimRecordList',
  data() {
    return {
      advanced: false,
      areaId: '', //区域
      requestUrl: 'api-d/depositInsClaims/expand/list',
      filterField: {
        projectId: '',
        projectName: '',
        areaId: '',
        industryId: '',
        investment: '',
        projectStatus: '',
        applyDateStart: '',
        applyDateEnd: '',
        claimsStatus: '',
        insureOrgStr: '',
        insureOrder: '',
        claimsOrder: '',
        insureType: '',
      },
      pageSize: 20,
      pageNo: 1,
      tableList: [],
      orderStatusOptions: [
        { id: 'LAUNCHED', name: '已发起' },
        { id: 'SUCCESS', name: '理赔完成' },
        { id: 'CHECKING', name: '材料复核中' },
      ],
      guaranteeInstitutionOptions: [],
      isSaveLoading: false,
      loading: false,
      isShow: false,
      userType: '',
      hiddleToolBar: false,
      tableHeight: null,
      bxUserType: '',
      typeOptions: [],
    }
  },
  computed: {
    ...mapState([
      'investmentOptions',
      'statusOptions',
      'areasOptions',
      'industryOptions',
    ]),
  },
  async created() {
    this.userType = this.$store.state.userType
    if (
      this.userType === 'PROJECT_ADMINISTRATOR' ||
      this.userType === 'LABOR_ADMINISTRATOR' ||
      this.userType === 'FENBAO_MANAGER'
    ) {
      this.filterField.projectId = this.$store.state.projectId
    }
    if (this.$store.state.userType == 'MONITOR') {
      this.bxUserType = 'JC'
    }
    await this.getUserType() //获取是否为保险业务员角色

    this.getGuaranteeInstitutionOptions()
    // this.getOrderStatusOptions();
    this.getSelectData()
    this.$nextTick(() => {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll, true)
    let that = this
    this.$nextTick(function () {
      let tableHeightCur = this.$refs.tableBox1.$el.clientHeight
      let tableTop = this.$refs.tableBox.getBoundingClientRect().top
      if (tableHeightCur + tableTop > window.innerHeight) {
        this.tableHeight =
          this.$refs.mainCon.getBoundingClientRect().height - 86
      } else {
        document.getElementsByClassName('tableBoxCur').length &&
          (document.getElementsByClassName('tableBoxCur')[0].style.height =
            window.innerHeight - tableTop - 28 + 'px')
      }

      window.onresize = function () {
        if (!that.$refs.tableBox1) {
          return
        }
        let tableHeightCur = that.$refs.tableBox1.$el.clientHeight
        let tableTop = that.$refs.tableBox.getBoundingClientRect().top
        if (tableHeightCur + tableTop > window.innerHeight) {
          that.tableHeight =
            that.$refs.mainCon.getBoundingClientRect().height - 86
        } else {
          document.getElementsByClassName('tableBoxCur').length &&
            (document.getElementsByClassName('tableBoxCur')[0].style.height =
              window.innerHeight - tableTop - 40 + 'px')
        }
      }
    })
  },
  activated() {
    window.addEventListener('scroll', this.handleScroll, true)
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
      this.$refs.tableBox1.doLayout()
    })
  },
  deactivated() {
    let that = this
    window.removeEventListener('scroll', that.handleScroll, true)
  },
  beforeDestroy() {
    let that = this
    window.removeEventListener('scroll', that.handleScroll, true)
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    // 保函类型
    getSelectData() {
      this.axios({
        method: 'post',
        url: 'api-bs/base/dicitem/findbytype',
        data: {
          type: 'DEPOSIT_INSURE_TYPE',
        },
      })
        .then((response) => {
          this.typeOptions = response.data.result
        })
        .catch((response) => {})
    },
    // 承保机构下拉
    getGuaranteeInstitutionOptions() {
      this.axios({
        method: 'post',
        url: 'api-d/insconfiginsurers/findByAllInsurers',
      })
        .then((response) => {
          if (!response.data.hasError) {
            this.guaranteeInstitutionOptions = response.data.result
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    // 列表
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 详情
    toDetail(row) {
      this.$router.push({
        path: '/home/newInsurance/claimsDetail',
        name: 'claimsDetail',
        query: {
          id: row.id,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/newInsurance/insuranceOrder',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    //获取选中的区域最后一级id
    getAreaId() {
      var areaId = this.areaId[this.areaId.length - 1]
      this.filterField.areaId = areaId ? areaId : ''
    },
    getStartTimeValue(val) {
      if (val) {
        this.filterField.applyDateStart = val
      } else {
        this.filterField.applyDateStart = ''
      }
    },
    getEndTimeValue(val) {
      if (val) {
        this.filterField.applyDateEnd = val
      } else {
        this.filterField.applyDateEnd = ''
      }
    },
    editClaims(row) {
      this.$router.push({
        path: '/home/newInsurance/addSettlementClaims',
        name: 'addSettlementClaims',
        query: {
          id: row.id,
          type: 'edit',
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/newInsurance/insuranceOrder',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    handleScroll(e) {
      this.$nextTick(function () {
        if (
          this.$refs.tableBox &&
          this.$refs.tableBox.getBoundingClientRect().top < 128
        ) {
          // this.tableHeight = this.$refs.mainCon.getBoundingClientRect().height - 84
          this.tableHeight = window.innerHeight - 216

          if (document.getElementsByClassName('tableBoxCur').length) {
            document.getElementsByClassName('tableBoxCur')[0].style.height =
              null
          }
          this.hiddleToolBar = true
        } else {
          this.hiddleToolBar = false
        }
      })
    },
    getUserType() {
      return this.axios({
        method: 'get',
        url: 'api-u/users/current',
      })
        .then((response) => {
          if (
            !response.data.hasError &&
            response.data.sysRoles &&
            response.data.sysRoles.length
          ) {
            response.data.sysRoles.forEach((v) => {
              if (v.code === 'INSURANCE_SALESMAN') {
                this.bxUserType = 'BX'
              }
            })
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
  },
}
</script>
<style lang="less" scoped>
.mainCon {
  top: 210px;
  padding: 0 0 16px !important;
  min-height: 600px;
}
@import '../../../static/css/filterArea.less';
</style>
